package com.gcsoftware.gateway.confLoader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yinpeng on 17/9/26.
 */
public class CfgLoader
{
    private static final Logger log = LoggerFactory.getLogger(CfgLoader.class);

    public static final String fileSep = System.getProperty("file.separator");

    private static final String CONF_CATALOGUE_NAME = "config";

    public static StringBuilder getFullPath() {
        File file = new File("");

        String spath = file.getAbsolutePath();

        StringBuilder sFullPath = new StringBuilder();


        sFullPath.append(spath);
        sFullPath.append(fileSep);
        return sFullPath;
    }
    public static String getCurrentCfgPath() {
        StringBuilder sTmp = getFullPath();
        sTmp.append("."+fileSep+CONF_CATALOGUE_NAME+fileSep);
        return sTmp.toString();
    }

    public static String getParentCfgPath() {
        StringBuilder sTmp = getFullPath();
        sTmp.append(".."+fileSep+CONF_CATALOGUE_NAME+fileSep);
        return sTmp.toString();
    }

    public static Properties cfgLoader(String CFG_NAME)
    {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        Properties prop = null;
        try
        {

            File cfgFile = null;
            cfgFile = new File(getCurrentCfgPath() + CFG_NAME);

            boolean bflag = false;

            if (cfgFile.exists() && cfgFile.isFile()) {
                fis = new FileInputStream(cfgFile);
                bflag = true;
            } else {
                cfgFile = new File(getParentCfgPath() + CFG_NAME);

                if (cfgFile.exists() && cfgFile.isFile()) {
                    fis = new FileInputStream(cfgFile);
                    bflag = true;
                } else {
                    throw new FileNotFoundException(
                            "cfgLoader can't find the file[" + CFG_NAME + "] at path of ./"+CONF_CATALOGUE_NAME+" or ../"+CONF_CATALOGUE_NAME+" ["
                                    + cfgFile.getAbsolutePath() + "]");
                }
            }

            isr = new InputStreamReader(fis);

            prop = new Properties();
            prop.load(isr);

            isr.close();
            fis.close();

            return prop;
        }
        catch (Exception e)
        {
            try
            {
                if (null != isr)
                {
                    isr.close();
                }
                if (null != fis)
                {
                    fis.close();
                }
            }
            catch (IOException ioe)
            {
                //Ignore the exception
            }

            e.printStackTrace();
            log.error("cfgLoder to load file {} throw exception", CFG_NAME, e);
            return null;
        }
    }
}
